package be.jaisy.project.ui.equinox;

import java.util.Collection;

import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import be.jaisy.project.service.api.IProjectService;
import be.jaisy.project.service.api.to.ProjectTo;

public class ProjectCommandProvider implements CommandProvider {

	private BundleContext context;

	public ProjectCommandProvider(BundleContext context) {
		super();
		this.context = context;
	}

	public String getHelp() {		
		StringBuffer buffer = new StringBuffer();
		buffer.append("\n\n---Controlling the Project services---\n");
		buffer.append("\tcreateProject (cp) - create project service\n");
		buffer.append("\tupdateProject (up) - update project service\n");
		buffer.append("\tgetProject (gp) - get project service\n");
		buffer.append("\tlistProject (lp) - list project service\n");
		return buffer.toString();

	}

	public void _cp(CommandInterpreter intp) throws Exception {
		_createProject(intp);
	}
	
	public void _createProject(CommandInterpreter intp) throws Exception {
		
		ServiceReference rf = context.getServiceReference(IProjectService.class.getName());
		if(rf==null){
			System.err.println("serviceReference for ["+IProjectService.class.getName()+"] not found");
			return;
		}
		IProjectService service = (IProjectService)context.getService(rf);
		
		ProjectTo projectTo = new ProjectTo();
		projectTo.setName(intp.nextArgument());
		projectTo.setDescription(intp.nextArgument());
		service.createProject(projectTo);
		System.out.println("createProject done" );
	}
	
	public void _up(CommandInterpreter intp) throws Exception {
		_updateProject(intp);
	}
	
	public void _updateProject(CommandInterpreter intp) throws Exception {
		
		ServiceReference rf = context.getServiceReference(IProjectService.class.getName());
		if(rf==null){
			System.err.println("serviceReference for ["+IProjectService.class.getName()+"] not found");
			return;
		}
		IProjectService service = (IProjectService)context.getService(rf);
		
		ProjectTo projectTo = new ProjectTo();
		projectTo.setName(intp.nextArgument());
		projectTo.setDescription(intp.nextArgument());
		service.updateProject(projectTo);
		System.out.println("updateProject done" );
	}
	
	public void _dp(CommandInterpreter intp) throws Exception {
		_deleteProject(intp);
	}
	
	public void _deleteProject(CommandInterpreter intp) throws Exception {
		
		ServiceReference rf = context.getServiceReference(IProjectService.class.getName());
		if(rf==null){
			System.err.println("serviceReference for ["+IProjectService.class.getName()+"] not found");
			return;
		}
		IProjectService service = (IProjectService)context.getService(rf);
		
		ProjectTo projectTo = new ProjectTo();
		projectTo.setName(intp.nextArgument());
		projectTo.setDescription(intp.nextArgument());
		service.deleteProject(projectTo);
		System.out.println("deleteProject done" );
	}
	
	public void _gp(CommandInterpreter intp) throws Exception {
		_getProject(intp);
	}
	
	public void _getProject(CommandInterpreter intp) throws Exception {
		
		ServiceReference rf = context.getServiceReference(IProjectService.class.getName());
		if(rf==null){
			System.err.println("serviceReference for ["+IProjectService.class.getName()+"] not found");
			return;
		}
		IProjectService service = (IProjectService)context.getService(rf);
		
		ProjectTo projectTo = service.getProject(intp.nextArgument());
		if(projectTo==null)
			System.out.println("getProject return null");
		System.out.println("getProject return :"+projectTo.toString());
	}
	
	
	public void _lp(CommandInterpreter intp) throws Exception {
		_listProject(intp);
	}
	
	@SuppressWarnings("unchecked")
	public void _listProject(CommandInterpreter intp) throws Exception {
		
		ServiceReference rf = context.getServiceReference(IProjectService.class.getName());
		if(rf==null){
			System.err.println("serviceReference for ["+IProjectService.class.getName()+"] not found");
			return;
		}
		IProjectService service = (IProjectService)context.getService(rf);
		
		Collection<ProjectTo> projectTos = service.listProject();
		if(projectTos==null)
			System.out.println("listProject return null");
		System.out.println("listProject return :"+projectTos.size()+" ProjectTo:");
		int cpt=0;
		for (ProjectTo to : projectTos) {
			System.out.println(cpt+" "+to.toString()+".");
			cpt++;
		}	
	}
}